import { BaseReactElementState, BaseReactElement, reactControl, BaseReactElementControl } from "pao-aop-client";
import React from "react";
import { addon } from "pao-aop";
import { edit_props_info } from "src/projects/app/util-tool";
import FormCreator, { InputType } from "src/business/components/buss-components/form-creator";
import { GetFieldDecoratorOptions } from "antd/lib/form/Form";
import { AppServiceUtility } from 'src/projects/app/appService';
import { Select } from "antd";
import { remote } from "src/projects/remote";

let { Option } = Select;
/**
 * 组件：组织机构输入组件状态
 */
export interface OrganizationEntryState extends BaseReactElementState {
    /** 行政区划列表 */
    administrative_division_list?: any;
    /** 基础数据 */
    base_data?: any;
    /** 组织机构列表 */
    org_list?: any[];
}

/**
 * 组件：组织机构输入组件
 * 组织机构输入组件
 */
export class OrganizationEntry extends BaseReactElement<OrganizationEntryControl, OrganizationEntryState> {
    constructor(props: any) {
        super(props);
        this.state = {
            administrative_division_list: [],
            base_data: { 'qualification_info': [] },
        };
    }
    // 校验手机号码
    checkPhone = (rule: any, value: any, callback: any) => {
        var phone = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/;
        if (value === '') {
            callback('请输入手机号码');
        }
        if (value.length !== 11) {
            callback('格式不正确');
            if (!phone.test(value)) {
                callback('格式不正确');
            }
        }
    }
    // 校验座机号码
    checkLinePhone = (rule: any, value: any, callback: any) => {
        var phone = /^0\d{2,3}-\d{7,8}$/;
        if (value === '') {
            callback('请输入固定电话号码');
        }
        if (value.length !== 11 && value.length !== 12) {
            callback('格式不正确');
            if (!phone.test(value)) {
                callback('格式不正确');
            }
        }
    }
    componentDidMount() {
        // AppServiceUtility.business_area_service.get_business_area_list!({})!
        //     .then((data: any) => {
        //         if (data.result) {
        //             this.setState({
        //                 administrative_division_list: data.result
        //             });
        //         }
        //     });
        AppServiceUtility.business_area_service.get_admin_division_list!({})!
            .then(data => {
                this.setState({
                    administrative_division_list: data!.result
                });
            })
            .catch(err => {
                console.info(err);
            });
        AppServiceUtility.person_org_manage_service.get_organization_tree_list!({})!
            .then(data => {
                this.setState({
                    org_list: data!.result
                });
            })
            .catch(err => {
                console.info(err);
            });
        let { id } = this.props;
        if (id) {
            let { service_option } = this.props;
            let operation_option = service_option.operation_option;
            AppServiceUtility.person_org_manage_service.get_organization_list_all!(...operation_option.query.arguments)!
                .then((data: any) => {
                    this.setState({
                        base_data: data.result[0]
                    });
                });
        }
    }

    render() {
        const org_type = ['福利院', '幸福院', '服务商', '平台', '民政'];
        const org_type_list: JSX.Element[] = org_type!.map((item) => <Option key={item} value={item}>{item}</Option>);
        const org_nature = ['公办', '公建民营', '民办企业', '民办非营利'];
        const org_nature_list: JSX.Element[] = org_nature!.map((item) => <Option key={item} value={item}>{item}</Option>);
        let { base_data } = this.state;
        let edit_props = {
            form_items_props: [
                {
                    title: "组织机构",
                    need_card: true,
                    input_props: [
                        {
                            type: InputType.antd_input,
                            label: "名称",
                            decorator_id: "name",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入名称" }],
                                initialValue: base_data ? base_data.name : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入名称"
                            }
                        }, {
                            type: InputType.antd_input,
                            label: "地址",
                            decorator_id: "address",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入地址" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.address : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入地址"
                            }
                        },
                        {
                            type: InputType.nt_rich_text,
                            label: "简介",
                            decorator_id: "describe",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入简介" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.describe : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入简介",
                                autocomplete: 'off',
                            }
                        },
                        {
                            type: InputType.upload,
                            label: "相关图片",
                            decorator_id: "picture_list",
                            field_decorator_option: {
                                rules: [{ required: false, message: "请上传营业执照" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.business_photo : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                action: remote.upload_url
                            }
                        }, {
                            type: InputType.antd_input,
                            label: "经度",
                            decorator_id: "lon",
                            field_decorator_option: {
                                rules: [{ required: false, message: "请输入经度" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.lon : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入经度"
                            }
                        }, {
                            type: InputType.antd_input,
                            label: "纬度",
                            decorator_id: "lat",
                            field_decorator_option: {
                                rules: [{ required: false, message: "请输入纬度" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.lat : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入纬度"
                            }
                        }, {
                            type: InputType.antd_input,
                            label: "法人",
                            decorator_id: "legal_person",
                            field_decorator_option: {
                                rules: [{ required: false, message: "请输入法人" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.legal_person : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入法人"
                            }
                        }, {
                            type: InputType.upload,
                            label: "营业执照",
                            decorator_id: "business_license_url",
                            field_decorator_option: {
                                initialValue: base_data.organization_info ? base_data.organization_info.business_license_url : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                action: remote.upload_url
                            }
                        }, {
                            type: InputType.antd_input,
                            label: "联系电话",
                            decorator_id: "telephone",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入联系电话" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.telephone : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入联系电话"
                            }
                        }, {
                            type: InputType.select,
                            label: "组织机构类型",
                            decorator_id: "personnel_category",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入组织机构类型" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.personnel_category : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入组织机构类型",
                                childrens: org_type_list

                            }
                        }, {
                            type: InputType.select,
                            label: "组织机构性质",
                            decorator_id: "organization_nature",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请输入组织机构性质" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.organization_nature : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入组织机构性质",
                                childrens: org_nature_list

                            }
                        }, {
                            type: InputType.tree_select,
                            label: "行政区划",
                            decorator_id: "admin_area_id",
                            field_decorator_option: {
                                rules: [{ required: true, message: "请选择行政区划" }],
                                initialValue: base_data ? base_data.admin_area_id : ''
                            } as GetFieldDecoratorOptions,
                            option: {
                                showSearch: true,
                                treeNodeFilterProp: 'title',
                                dropdownStyle: { maxHeight: 400, overflow: 'auto' },
                                allowClear: true,
                                treeDefaultExpandAll: true,
                                treeData: this.state.administrative_division_list,
                                placeholder: "请选择行政区划",
                            },
                        }, {
                            type: InputType.tree_select,
                            label: "上级组织机构",
                            decorator_id: "super_org_id",
                            field_decorator_option: {
                                rules: [{ required: true, message: "上级组织机构" }],
                                initialValue: base_data.organization_info ? base_data.organization_info.super_org_id : '7e7e2fec-d91d-11e9-8e1d-983b8f0bcd67',
                            } as GetFieldDecoratorOptions,
                            option: {
                                showSearch: true,
                                treeNodeFilterProp: 'title',
                                dropdownStyle: { maxHeight: 400, overflow: 'auto' },
                                allowClear: true,
                                treeDefaultExpandAll: true,
                                treeData: this.state.org_list,
                                placeholder: "请选择上级组织机构",
                            },
                        }, {
                            type: InputType.text_area,
                            label: "备注",
                            decorator_id: "comment",
                            field_decorator_option: {
                                initialValue: base_data.organization_info ? base_data.organization_info.comment : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入备注",
                                row: 3,
                            }
                        },
                        // {
                        //     type: InputType.objectFieldList,
                        //     label: "资质信息",
                        //     decorator_id: "qualification_info",
                        //     field_decorator_option: {
                        //         rules: [],
                        //         initialValue: base_data['qualification_info'].length > 0 ? base_data['qualification_info'] : ''
                        //     } as GetFieldDecoratorOptions,
                        //     option: {
                        //         befor_placeholder: "资质名称",
                        //         after_placeholder: '资质等级',
                        //         before_key: 'name',
                        //         after_key: 'qualification_level',
                        //     }
                        // }
                        {
                            type: InputType.upload,
                            label: "统一社会信用代码证",
                            decorator_id: "unified_social_credit",
                            field_decorator_option: {
                                initialValue: base_data.organization_info ? base_data.organization_info.unified_social_credit : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                action: remote.upload_url
                            }
                        },
                        {
                            type: InputType.upload,
                            label: "平台备案承诺书",
                            decorator_id: "platform_filing_commitment",
                            field_decorator_option: {
                                initialValue: base_data.organization_info ? base_data.organization_info.platform_filing_commitment : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                action: remote.upload_url
                            }
                        },
                        {
                            type: InputType.text_area,
                            label: "服务协议",
                            decorator_id: "service_agreement",
                            field_decorator_option: {
                                initialValue: base_data.organization_info ? base_data.organization_info.service_agreement : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入服务协议",
                                row: 3,
                            }
                        },
                        {
                            type: InputType.rate,
                            label: "星级",
                            decorator_id: "star_level",
                            field_decorator_option: {
                                initialValue: base_data.organization_info ? base_data.organization_info.star_level : '',
                            } as GetFieldDecoratorOptions,
                            option: {
                                placeholder: "请输入星级"
                            }
                        },
                    ]
                }
            ],
            other_btn_propps: [
                ...this.props.other_btn_propps!
            ],
            submit_btn_propps: this.props.submit_btn_propps,
            service_option: this.props.service_option,
            id: this.props.id,
        };
        let edit_props_list = Object.assign(edit_props, edit_props_info);
        return (
            <div>
                <FormCreator {...edit_props_list} />
            </div>
        );
    }
}

/**
 * 控件：组织机构输入组件控制器
 * 组织机构输入组件
 */
@addon('OrganizationEntry', '组织机构输入组件', '组织机构输入组件')
@reactControl(OrganizationEntry)
export class OrganizationEntryControl extends BaseReactElementControl {
    /** 组织机构id */
    id?: string;
    /** 基本数据 */
    public base_data?: any;
    /** 提交按钮 */
    public submit_btn_propps?: any;
    /** 其他按钮 */
    public other_btn_propps?: any[];
    /** 服务配置 */
    public service_option?: any;
}